﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VatTuDTO;
using MySql.Data.MySqlClient;
using System.Data;

namespace VatTuDAO
{
    public class NguoiDungDAO :BaseDataProvider
    {
        MySqlCommand _cmd;
        // Lay danh sach nguoi dung 
        public NguoiDungDTO SelectNguoiDung(string tenDangNhap,string matKhau)   
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from NGUOIDUNG where TenDangNhap= @TenDangNhap and MatKhau= @MatKhau";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@TenDangNhap",tenDangNhap));
            _cmd.Parameters.Add(new MySqlParameter("@MatKhau",matKhau));


            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            NguoiDungDTO nd = new NguoiDungDTO();

            try
            {

                DataRow row = dt.Rows[0];//kết quả trả ra ở dòng 0

               
                nd.MaNguoiDung= int.Parse(row["MaNguoiDung"].ToString());
                nd.TenNguoiDung = row["TenNguoiDung"].ToString();
                nd.GioiTinh = row["GioiTinh"].ToString();
                nd.CMND = row["CMND"].ToString();
                nd.DiaChi = row["DiaChi"].ToString();
                nd.DienThoai = row["DienThoai"].ToString();
                nd.Email = row["Email"].ToString();
                nd.TenDangNhap = row["TenDangNhap"].ToString();
                nd.MatKhau = row["MatKhau"].ToString();
                nd.MaLoaiNguoiDung=int.Parse(row["MaLoaiNguoiDung"].ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return nd;
        }
        // Lay danh sach nguoi dung 
        public List<NguoiDungDTO> SelectNguoiDung()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from NGUOIDUNG ";
            _cmd.CommandType = CommandType.Text;
        


            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            NguoiDungDTO nd;
            List<NguoiDungDTO> ds = new List<NguoiDungDTO>();
            try
            {

                foreach (DataRow row in dt.Rows)
                {
                    nd = new NguoiDungDTO();
                    nd.MaNguoiDung = int.Parse(row["MaNguoiDung"].ToString());
                    nd.TenNguoiDung = row["TenNguoiDung"].ToString();
                    nd.GioiTinh = row["GioiTinh"].ToString();
                    nd.CMND = row["CMND"].ToString();
                    nd.DiaChi = row["DiaChi"].ToString();
                    nd.DienThoai = row["DienThoai"].ToString();
                    nd.Email = row["Email"].ToString();
                    nd.TenDangNhap = row["TenDangNhap"].ToString();
                    nd.MatKhau = row["MatKhau"].ToString();
                    nd.MaLoaiNguoiDung = int.Parse(row["MaLoaiNguoiDung"].ToString());
                    ds.Add(nd);

                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ds;
        }
        // Lay mot nguoi dung co ma nguoi dung 
        public NguoiDungDTO SelectNguoiDung(int MaNguoiDung)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from NguoiDung where MaNguoiDung=@MaNguoiDung";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaNguoiDung", MaNguoiDung));
            DataTable dt = new DataTable();
            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            NguoiDungDTO dto = new NguoiDungDTO();

            try
            {

                DataRow row = dt.Rows[0];//kết quả trả ra ở dòng 0

                dto.MaNguoiDung = int.Parse(row["MaNguoiDung"].ToString());
                dto.TenNguoiDung = row["TenNguoiDung"].ToString();
                dto.GioiTinh = row["GioiTinh"].ToString();
                dto.CMND = row["CMND"].ToString();
                dto.DiaChi = row["DiaChi"].ToString();
                dto.DienThoai = row["DienThoai"].ToString();
                dto.Email = row["Email"].ToString();
                dto.TenDangNhap = row["TenDangNhap"].ToString();
                dto.MatKhau = row["MatKhau"].ToString();
                dto.MaLoaiNguoiDung = int.Parse(row["MaLoaiNguoiDung"].ToString());

                


            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dto;
        }

        public Boolean InsertNguoiDung(NguoiDungDTO NguoiDung)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "insert into NguoiDung(TenNguoiDung,GioiTinh,CMND,DiaChi,DienThoai,Email,TenDangNhap,MatKhau,MaLoaiNguoiDung) values(@TenNguoiDung,@GioiTinh,@CMND,@DiaChi,@DienThoai,@Email,@TenDangNhap,@MatKhau,@MaLoaiNguoiDung)";
            _cmd.CommandType = CommandType.Text;

           
            _cmd.Parameters.Add(new MySqlParameter("@TenNguoiDung", NguoiDung.TenNguoiDung));
            _cmd.Parameters.Add(new MySqlParameter("@GioiTinh", NguoiDung.GioiTinh));
            _cmd.Parameters.Add(new MySqlParameter("@CMND", NguoiDung.CMND));
            _cmd.Parameters.Add(new MySqlParameter("@DiaChi", NguoiDung.DiaChi));
            _cmd.Parameters.Add(new MySqlParameter("@DienThoai", NguoiDung.DienThoai));
            _cmd.Parameters.Add(new MySqlParameter("@Email", NguoiDung.Email));
            _cmd.Parameters.Add(new MySqlParameter("@TenDangNhap", NguoiDung.TenDangNhap));
            _cmd.Parameters.Add(new MySqlParameter("@MatKhau", NguoiDung.MatKhau));
            _cmd.Parameters.Add(new MySqlParameter("@MaLoaiNguoiDung", NguoiDung.MaLoaiNguoiDung));
           

            return ExecuteNonQuery(_cmd);
        }

        // update Nguoi dung 
        
        public Boolean UpdateNguoiDung(NguoiDungDTO nd)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update NguoiDung set TenNguoiDung=@TenNguoiDung,GioiTinh=@GioiTinh, " ;
            _cmd.CommandText += "CMND=@CMND,DiaChi= @DiaChi,DienThoai=@DienThoai,Email=@Email,TenDangNhap=@TenDangNhap,";
             _cmd.CommandText +="MatKhau=@MatKhau,MaLoaiNguoiDung=@MaLoaiNguoiDung where MaNguoiDung=@MaNguoiDung";
            _cmd.CommandType = CommandType.Text;

            _cmd.Parameters.Add(new MySqlParameter("@TenNguoiDung", nd.TenNguoiDung));
            _cmd.Parameters.Add(new MySqlParameter("@GioiTinh", nd.GioiTinh));
            _cmd.Parameters.Add(new MySqlParameter("@CMND", nd.CMND));
            _cmd.Parameters.Add(new MySqlParameter("@DiaChi", nd.DiaChi));
            _cmd.Parameters.Add(new MySqlParameter("@DienThoai", nd.DienThoai));
            _cmd.Parameters.Add(new MySqlParameter("@Email", nd.Email));
            _cmd.Parameters.Add(new MySqlParameter("@TenDangNhap", nd.TenDangNhap));
            _cmd.Parameters.Add(new MySqlParameter("@MatKhau", nd.MatKhau));
            _cmd.Parameters.Add(new MySqlParameter("@MaLoaiNguoiDung", nd.MaLoaiNguoiDung));
            _cmd.Parameters.Add(new MySqlParameter("@MaNguoiDung", nd.MaNguoiDung));

                   
            return ExecuteNonQuery(_cmd);
        }

        public Boolean DeleteNguoiDung(int MaNguoiDung)
        {

            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete from NguoiDung";
            _cmd.CommandText += " where MaNguoiDung=@MaNguoiDung";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaNguoiDung", MaNguoiDung));

            if (ExecuteNonQuery(_cmd) == false)
            {
                return false;
            }

            return true;
        }
         



        
    }
}
